package com.ext.aic.ui;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.ext.aic.R;
import com.ext.aic.adapter.WhiteListAdapter;
import com.ext.aic.beans.WhiteListItem;
import com.ext.aic.constants.EBusConstants;
import com.ext.aic.databinding.WeChatWhiteListLayoutBinding;
import com.ext.aic.utils.RedirectUtils;
import com.ext.aic.utils.WhiteListUtils;
import com.eyinfo.android_pure_utils.ebus.EBus;
import com.eyinfo.android_pure_utils.ebus.SubscribeEBus;
import com.eyinfo.android_pure_utils.utils.ObjectJudge;

import java.util.List;

public class WeChatWhiteListActivity extends BaseActivity {
    private WeChatWhiteListLayoutBinding binding;
    private WhiteListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = View.inflate(this, R.layout.we_chat_white_list_layout, null);
        binding = WeChatWhiteListLayoutBinding.bind(view);
        setContentView(binding.getRoot());
        EBus.getInstance().registered(this);
        bindView();
        bindData();
    }

    private void bindView() {
        binding.topView.backIv.setOnClickListener(v -> finish());
        binding.topView.titleTv.setText(R.string.white_list_setting);
        binding.topView.confirmTv.setVisibility(View.VISIBLE);
        binding.topView.confirmTv.setText(R.string.add);
        binding.topView.confirmTv.setOnClickListener(this::onAddWhiteListClick);
    }

    @SuppressLint("NotifyDataSetChanged")
    private void bindData() {
        List<WhiteListItem> items = WhiteListUtils.getInstance().getWhiteList();
        if (ObjectJudge.isNullOrEmpty(items)) {
            binding.emptyLl.setVisibility(View.VISIBLE);
            binding.refreshListSrl.setVisibility(View.GONE);
        } else {
            binding.emptyLl.setVisibility(View.GONE);
            binding.refreshListSrl.setVisibility(View.VISIBLE);
        }

        binding.whiteListRv.setLayoutManager(new LinearLayoutManager(this));
        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(this, R.drawable.divider_transparent));
        binding.whiteListRv.addItemDecoration(divider);
        adapter = new WhiteListAdapter(this);
        binding.whiteListRv.setAdapter(adapter);
        adapter.addItems(items);
        adapter.notifyDataSetChanged();
    }

    @SuppressLint("NotifyDataSetChanged")
    @SubscribeEBus(receiveKey = EBusConstants.refreshWhileList)
    public void onRefreshWhileListEvent() {
        binding.emptyLl.setVisibility(View.GONE);
        binding.refreshListSrl.setVisibility(View.VISIBLE);
        List<WhiteListItem> items = WhiteListUtils.getInstance().getWhiteList();
        adapter.clear();
        adapter.addItems(items);
        adapter.notifyDataSetChanged();
    }

    public void onAddWhiteListClick(View view) {
        RedirectUtils.getInstance().startActivity(this, AddWhiteListActivity.class);
    }
}
